package com.docking.heylo.web.controller;

import com.docking.heylo.dao.entity.Protocol;
import com.docking.heylo.dao.entity.ProtocolExample;
import com.docking.heylo.dao.mapper.ProtocolMapper;
import com.docking.heylo.exception.GeneralException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/protocol")
public class ProtocolController {

    @Resource
    private ProtocolMapper protocolMapper;

    private static final String htmlTml = "<!DOCTYPE html><html lang=\"zh-CN\"><head><title th:text=\"${name}\"></title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no\"><style>#content::-webkit-scrollbar{display:none}body,html{height:100%;width:100%;padding:5px 7px 40px 7px;margin:0;box-sizing:border-box}#content{width:100%;height:100%;padding:0}.r{position:fixed;bottom:0;background-color:#0ae;width:100%;height:8%;text-align:center;line-height:48px}.r a{color:#fff;text-decoration:none}a{-webkit-tap-highlight-color:rgba(255,255,255,0);-webkit-user-select:none;-moz-user-focus:none;-moz-user-select:none}img{max-width:100%}.elegant-aero{padding:10px 10px 50px 10px}.elegant-aero div{margin:5px 0}.elegant-aero input[type=email],.elegant-aero input[type=text],.elegant-aero select,.elegant-aero textarea{display:block;width:98%;height:30px;line-height:1.3;border-width:1px;border-style:solid;background-color:#fff;border-radius:2px;border-color:#e6e6e6;font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit;outline:0;margin-top:3px;padding-left:5px}</style></head><body><div id=\"content\" th:utext=\"${content}\"></div></body></html>";


    /***
     * 查看协议
     *
     * @param code
     * @param userId
     * @return
     */
    @RequestMapping("/{code}.html")
    @ResponseBody
    public String show(@PathVariable String code, Integer userId){
        ProtocolExample example = new ProtocolExample();
        ProtocolExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(code);

        List<Protocol> protocols = protocolMapper.selectByExampleWithBLOBs(example);
        Protocol protocol = protocols.size() == 0 ? null : protocols.get(0);
        if(protocol == null){
            throw new GeneralException("协议不存在");
        }

        Map<String, Object> params = new HashMap<String, Object>(){{
            put("name",     protocol.getName());
            put("content",  protocol.getContent());
        }};
        TemplateEngine templateEngine = new TemplateEngine();
        Context context = new Context();
        context.setVariables(params);
        return templateEngine.process(htmlTml,context);
    }








}
